package edu.maping.easy.painter.plugins;

import controlP5.ControlP5;
import processing.core.PApplet;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.model.PhysicPolyThing;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.model.World;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.painter.napplet.EMNAppletAdapter;
import edu.maping.easy.sequence.ActionConf;
import edu.maping.easy.sequence.ticker.TickerListener;

public class RectangleTransformEMNApplet extends EMNAppletAdapter implements
		TickerListener {

	int moment = 0;

	boolean asc = false;

	int time = 0;

	int blue = 0;
	int green = 0;
	int red = 0;
	int decay = 5;
	int rise = 20;
	
	public RectangleTransformEMNApplet() {
		 rise = 20;
	}

	public void setup() {
		size(200, 200, P2D);
		ligth();
	}
	@Override
	public void setThing(Thing thing) {
		this.thing = thing;

	}

	public void draw() {

		background(0, 0);

		fill(255);
		ellipse(mouseX, mouseY, 5, 5);
		if (mousePressed) {
			ellipse(mouseX, mouseY, 5, 5);
			return;
		}
		
		time++;

		if (thing == null) {
			return;
		}

		if (thing.isEvent()) {
			turnOn();
			thing.setEvent(false);
		}

		if (asc) {
			if (moment < 255) {
				moment += rise;
			} else {
				asc = false;
				moment = 255;
			}
		} else {
			if (moment > 0) {
				moment -= decay;
			} else {
				moment = 0;
				return;
			}
		}

		float tempR = (red / 255F) * moment;
		float tempG = (green / 255F) * moment;
		float tempB = (blue / 255F) * moment;

		PolygonThing poly = (PolygonThing) thing;
		pushStyle();
		beginShape();
		strokeWeight(0);
		stroke(0);
		//fill(tempR, tempG, tempB);
//		for (int i = 0; i < poly.getVertex().size(); i++) {
//			Point point = poly.getVertex().get(i);
//			vertex(point.x, point.y);
//		}
		
		vertex(0, 0);
		vertex(0, 0);
		vertex(0, 0);
		
		endShape();
		popStyle();
		
		ellipse(10, 10, 25, 25);

	}

	public void turnOn() {
		asc = true;
	}

	@Override
	public String getName() {
		return "Pulso";
	}

	protected void setParameters(String name, float value) {

		if (name.equals("RED")) {
			red = (int) value;
		} else if (name.equals("GREEN")) {
			green = (int) value;
		} else if (name.equals("BLUE")) {
			blue = (int) value;
		} else if (name.equals("DECAY")) {
			decay = (int) value;
		} else if (name.equals("RISE")) {
			rise = (int) value;
		}
	}

	@Override
	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {

		super.setup(pApplet, controlFacade, buttonList, guiFactory);
		utilPainter.add("RED", 0, 255);
		utilPainter.add("GREEN", 0, 255);
		utilPainter.add("BLUE", 0, 255);
		utilPainter.add("DECAY", 1, 40);
		utilPainter.add("RISE", 1, 80);

	}
	
	@Override
	public ThingPainterDelegate createNewInstance() {

		Object object = parentNAppletManager.createEmbeddedNApplet(this
				.getClass().getName(), 200, 200);
		RectangleTransformEMNApplet contournTransform = (RectangleTransformEMNApplet) object;
		contournTransform.utilPainter = utilPainter.clone();
		return contournTransform;
	}

}
